<html>

<head>
    <meta charset="utf-8">
</head>

<body>
    <h1>sciter的@sys模块使用</h1>
    <div#app>
        </div>
        <div#watch>watch状态</div>
         
</body>
<script type="module">
    import * as sys from "@sys";
    console.log("sys", sys);
    // 基于libuv实现的类nodejs文件,网络,进程,其他
    // 文件 http://nodejs.cn/api/fs.html
    let fs = sys.fs;
    console.log("cwd", sys.cwd());

    // 不支持callback形式
    function testRead() {
        // 类似nodejs的fs操作  不支持callback
        // 实现为promise
        fs.open("test.txt")
            .then(file => {
                return file.read();
            }).then(con => {
                console.log("cc", decode(con));
            })
    }
    // testRead();
    function testStat() {
        fs.stat("test.txt").then(res => {
            let app = document.$("#app");
            app.append(<p>{res.st_blksize}</p>)
            let size = res.st_blksize;
            app.append(<p>{size}</p>)
            Object.keys(res).forEach(e => {
                app.append(<p>{e}:   {res[e]}</p>)
            })
        })

        let stat = fs.$stat("test.txt");
        console.log("stat", Object.keys(stat));

        let stat2 = fs.$lstat("test.txt");
        console.log("stat2", Object.keys(stat2));
    }

    function testReapath() {
        let rp = fs.realpath("test.txt").then(res => {
            console.log("rp", res);
        });
    }

    function testUnlink() {
        fs.unlink("del.txt").then((...res) => {
            console.log("unlink213", res);
        }, err => {
            console.log("unlink err", err);
        });
    }

    function testRename() {
        fs.rename("test.txt", "dd.txt").then((...res) => {
            console.log("rename", res);
        })
    }

    function testRead2() {
        fs.readdir(".").then((...res) => {
            console.log('readdir', res);
        }, err => {
            console.log("dd", err);
        })

        fs.readfile("test.txt").then((...res) => {
            console.log('readfile', res);
        }, err => {
            console.log("dd", err);
        })

        let a = fs.$mkdir("ddd");
        console.log("a", a);
    }

    // 例子直接在gitee仓库下载查看
    function testWatch() {
        let w = fs.watch(sys.cwd(), ((...res) => {
            console.log("ddd", res);
        }))

        console.log("w", w);

        let wdiv = document.$("#watch");
        wdiv.content(<span>watching...</span>);

        wdiv.on("click", () => {
            w.close();
            wdiv.content(<span>no watch</span>);
        })


        let info = fs.splitpath(sys.cwd());
        console.log("info", info);
        document.$("#app").content(<div><p>{info[0]}</p><p>{info[1]}</p></div>);
    }

   
</script>

</html>